﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Linkbase;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The and-filter can be used to match facts based upon criteria expressed by each one of its sub-filters. 
    /// </summary>
    public class AndFilter : BooleanFilter
    {        
        /// <summary>
        /// Constructor.
        /// </summary>
        public AndFilter(Resource resource) : base(resource)
        {                        
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            List<List<FactItem>> itemListTemp = new List<List<FactItem>>();

            if (SubFilters.Count == 0)
            {
                return new List<FactItem>();
            }

            foreach (Filter subFilter in SubFilters)
            {
                //itemListTemp.Add(subFilter.EvaluateFilter(variableSet, itemsToFilter, dts));
                itemsToFilter = subFilter.EvaluateFilter(variableSet, itemsToFilter, dts);
            }

            //List<FactItem> intersectItems = null;

            //for (var i = 0; i < itemListTemp.Count - 1; i++)
            //{
            //    if (i == 0)
            //    {
            //        intersectItems = itemListTemp[i];
            //    }

            //    intersectItems = intersectItems.Intersect(itemListTemp[i + 1]).ToList();
            //}

            //return intersectItems;

            return itemsToFilter;
        }
    }
}
